<?php
/**
 * Created by PhpStorm.
 * User: DongYao
 * Date: 2017/11/25
 * Time: 下午7:40
 */

namespace XmtApp\Modules\Admin\Controllers\Api;

use App\Http\Resources\RolesResource;
use XmtApp\Modules\Admin\Controllers\Controller;

use App\Tables\Role;
use Illuminate\Http\Request;

class RoleController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取角色列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $table = new Role();

        if ($request->has('page')) {
            $data = $table->paginate($request->header('pageSize'));
        } else {
            $data = $table->get();
        }

        return new RolesResource($data);
    }

    /**
     * 删除指定的角色
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($id)
    {
        $res = Role::where('id', $id)->delete();

        if ($res) {
            $resp = ajax_return(true, '删除成功', []);
        } else {
            $resp = ajax_return(false, '删除失败', []);
        }

        return response()->json($resp);
    }
}